package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Shenheyuanyin;
import com.ruoyi.system.mapper.FenleiMapper;
import com.ruoyi.system.domain.Fenlei;
import com.ruoyi.system.service.IFenleiService;

/**
 * 商品分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-10
 */
@Service
public class FenleiServiceImpl implements IFenleiService 
{
    @Autowired
    private FenleiMapper fenleiMapper;

    /**
     * 查询商品分类
     * 
     * @param flId 商品分类主键
     * @return 商品分类
     */
    @Override
    public Fenlei selectFenleiByFlId(Long flId)
    {
        return fenleiMapper.selectFenleiByFlId(flId);
    }

    /**
     * 查询商品分类列表
     * 
     * @param fenlei 商品分类
     * @return 商品分类
     */
    @Override
    public List<Fenlei> selectFenleiList(Fenlei fenlei)
    {
        return fenleiMapper.selectFenleiList(fenlei);
    }

    @Override
    public List<Fenlei> selectFenleiListAll(Fenlei fenlei) {
        return fenleiMapper.selectFenleiListAll(fenlei);
    }

    /**
     * 新增商品分类
     * 
     * @param fenlei 商品分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFenlei(Fenlei fenlei)
    {
        int rows = fenleiMapper.insertFenlei(fenlei);
        insertShenheyuanyin(fenlei);
        return rows;
    }

    /**
     * 修改商品分类
     * 
     * @param fenlei 商品分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFenlei(Fenlei fenlei)
    {
        fenleiMapper.deleteShenheyuanyinByShyyId(fenlei.getFlId());
        insertShenheyuanyin(fenlei);
        return fenleiMapper.updateFenlei(fenlei);
    }

    /**
     * 批量删除商品分类
     * 
     * @param flIds 需要删除的商品分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFenleiByFlIds(Long[] flIds)
    {
        fenleiMapper.deleteShenheyuanyinByShyyIds(flIds);
        return fenleiMapper.deleteFenleiByFlIds(flIds);
    }

    /**
     * 删除商品分类信息
     * 
     * @param flId 商品分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFenleiByFlId(Long flId)
    {
        fenleiMapper.deleteShenheyuanyinByShyyId(flId);
        return fenleiMapper.deleteFenleiByFlId(flId);
    }

    /**
     * 新增审核原因信息
     * 
     * @param fenlei 商品分类对象
     */
    public void insertShenheyuanyin(Fenlei fenlei)
    {
        List<Shenheyuanyin> shenheyuanyinList = fenlei.getShenheyuanyinList();
        Long flId = fenlei.getFlId();
        if (StringUtils.isNotNull(shenheyuanyinList))
        {
            List<Shenheyuanyin> list = new ArrayList<Shenheyuanyin>();
            for (Shenheyuanyin shenheyuanyin : shenheyuanyinList)
            {
                shenheyuanyin.setShyyId(flId);
                list.add(shenheyuanyin);
            }
            if (list.size() > 0)
            {
                fenleiMapper.batchShenheyuanyin(list);
            }
        }
    }
}
